// pages/user/acti/detail/detail.js
import dayjs from 'dayjs';
import { actiStatEnum } from '../../../common/enums';
import { getFileType } from '../../../utils/file';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    actiId: null,
    actiInfo: {},
  },
  getScrollHeight() {
    // 获取内容区域的高度
    wx.createSelectorQuery().selectViewport().scrollOffset((res) => {
      this.setData({ scrollHeight: res.scrollHeight + 'px' });
    }).exec()
  },
  queryActiDetail() {
    const param = {
      actiId: this.data.actiId
    }
    getApp().callContainer('/api/actiQuery/query', param, (res) => {
      if (res.data.data) {
        res.data.data.actiStat_label = actiStatEnum.get(res.data.data.actiStatus);
        res.data.data.actiStat_color = this.getActiStatColor(res.data.data.actiStatus);
        res.data.data.imgUrlList = res.data.data.actiImgUrlList.split(',');
        res.data.data.actiBeginTime_label = dayjs(res.data.data.actiBeginTime).format('YYYY-MM-DD HH:mm:ss');
        this.setData({actiInfo: res.data.data})
        this.loadTmpFile();
        setTimeout(()=>{
          this.getScrollHeight();
        }, 500)
      }
    });
  },
  // 活动状态展示不同颜色
  getActiStatColor(actiStat) {
    if (actiStat == 2) {
      return 'bg-gradual-dd-orange';
    } else if (actiStat == 3) {
      return 'bg-gradual-dd-green';
    } else if (actiStat == 4) {
      return 'bg-gradual-dd-purple';
    } else if (actiStat == 5) {
      return 'bg-gradual-dd-red';
    } 
  },
  loadTmpFile() {
    if (this.data.actiInfo.imgUrlList) {
      const tmpUrlList = [];
      for (const imgUrl of this.data.actiInfo.imgUrlList) {
        getApp().getTempFilePath(imgUrl).then((res) => {
          const type = getFileType(imgUrl);
          tmpUrlList.push({ url: res, type: type });
          if (tmpUrlList.length == this.data.actiInfo.imgUrlList.length) {
            this.setData({ 'actiInfo.tmpUrlList': tmpUrlList });
          }
        })
      }
    }
  },
  onClickSwiperImage(event) {
    const url = event.currentTarget.dataset.operation;
    const urls = [];
    for (const item of this.data.actiInfo.tmpUrlList) {
      if (item.type == 'image') {
        urls.push(item.url);
      }
    }
    wx.previewImage({
      current: url,
      urls: urls
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options) {
      this.setData({ actiId: options.actiId })
      this.queryActiDetail();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})